package samse.exam.test1;

import android.app.ListActivity;
import java.util.ArrayList;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.AdapterContextMenuInfo;


public class AdaptListActivity2 extends ListActivity {
	private static final int MNU_TEST1 = 1;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main2);
		ArrayList<Person> m_orders = new ArrayList<Person>();
		 
		Person p1 = new Person("안드로이드", "011-123-4567"); // 리스트에 추가할 객체입니다.
		Person p2 = new Person("구글", "02-123-4567"); // 리스트에 추가할 객체입니다.
		Person p3 = new Person("", "02-123-4567"); // 리스트에 추가할 객체입니다.
		 
		m_orders.add(p1); // 리스트에 객체를 추가합니다.
		m_orders.add(p2); // 리스트에 객체를 추가합니다.
		m_orders.add(p3); // 리스트에 객체를 추가합니다.
		 
		PersonAdapter m_adapter = new PersonAdapter(this, R.layout.row2, m_orders); // 어댑터를 생성합니다.
		setListAdapter(m_adapter); //
		
		ListView lv = (ListView)findViewById(android.R.id.list);
		registerForContextMenu(lv);
	}

	public void onCreateContextMenu (ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
		AdapterView.AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
		
		menu.setHeaderTitle("ContextMenu");		
		menu.add(0, MNU_TEST1, 0, "menu1");
		
	}     

	public boolean onContextItemSelected(MenuItem item){
		//AdapterView.AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
		//final ListItems contextItem = (ListItems) m_adapter.getItem(info.position);
		//if(contextItem == null) return true;

		switch(item.getItemId()){
		case MNU_TEST1:
			break;
		}
		return true;
	}
  	
	
	private class PersonAdapter extends ArrayAdapter<Person> {
	 
		private ArrayList<Person> items;
		 
		public PersonAdapter(Context context, int textViewResourceId, ArrayList<Person> items) {
			super(context, textViewResourceId, items);
			this.items = items;
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;	
			if (v == null) {
				LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.row2, null);
			}
			Person p = items.get(position);
			if (p != null) {
				ImageView iv = (ImageView) v.findViewById(R.id.Icon);
				if(iv!=null) {
					iv.setImageResource(R.drawable.icon);
				}
				TextView tt = (TextView) v.findViewById(R.id.text1);
				if (tt != null){
					tt.setText(p.getName());                           
				}
			}
			return v;
		}
	}
	class Person {
	 
		private String Name;
		private String Number;
		 
		public Person(String _Name, String _Number){
			this.Name = _Name;
			this.Number = _Number;
		}
		 
		public String getName() {
			return Name;
		}
		 
		public String getNumber() {
			return Number;
		}
	 
	}
}
